{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create a SageMaker Studio Domain\n",
    "With the following configuration:\n",
    "* Within a VPC\n",
    "* Disable Internet Access (`VpcOnly`)\n",
    "* Encryption with KMS Keys for Elastic File System (EFS) notebook storage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sagemaker.create_domain(DomainName='default', \n",
    "                        AuthMode='IAM',\n",
    "                        DefaultUserSettings={\n",
    "                            'ExecutionRole': <ROLE_ARN>, \n",
    "                            'SecurityGroups': <SECURITY_GROUP_IDS>,\n",
    "                        },\n",
    "                        SubnetIds='<SUBNET_IDS>',\n",
    "                        VpcId='<VPC_ID>',\n",
    "                        AppNetworkAccessType='VpcOnly',\n",
    "                        KmsKeyId='<EFS_KMS_KEY_ID>')\n",
    "\n",
    "domain_status = ''\n",
    "while (domain_status != 'InService'):\n",
    "    time.sleep(10)\n",
    "    domain_status = sagemaker.list_domains()['Domains'][0]['Status']\n",
    "\n",
    "domain_id = sagemaker.list_domains()['Domains'][0]['DomainId']\n",
    "\n",
    "sagemaker.create_user_profile(DomainId=domain_id,\n",
    "                              UserProfileName='default')\n",
    "\n",
    "user_profile_status = ''\n",
    "while (user_profile_status != 'InService'):\n",
    "    time.sleep(10)\n",
    "    user_profile_status = sagemaker.describe_user_profile(DomainId=domain_id,\n",
    "                                                          UserProfileName='default')['Status']\n",
    "\n",
    "sagemaker.create_app(DomainId=domain_id,\n",
    "                     UserProfileName='default',\n",
    "                     AppType='JupyterServer',\n",
    "                     AppName='default')\n",
    "\n",
    "app_status = ''\n",
    "while (app_status != 'InService'):\n",
    "    time.sleep(10)\n",
    "    app_status = sagemaker.describe_app(DomainId=domain_id,\n",
    "                                        UserProfileName='default',\n",
    "                                        AppType='JupyterServer',\n",
    "                                        AppName='default')['Status']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Release Resources"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%html\n",
    "\n",
    "<p><b>Shutting down your kernel for this notebook to release resources.</b></p>\n",
    "<button class=\"sm-command-button\" data-commandlinker-command=\"kernelmenu:shutdown\" style=\"display:none;\">Shutdown Kernel</button>\n",
    "        \n",
    "<script>\n",
    "try {\n",
    "    els = document.getElementsByClassName(\"sm-command-button\");\n",
    "    els[0].click();\n",
    "}\n",
    "catch(err) {\n",
    "    // NoOp\n",
    "}    \n",
    "</script>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%javascript\n",
    "\n",
    "try {\n",
    "    Jupyter.notebook.save_checkpoint();\n",
    "    Jupyter.notebook.session.delete();\n",
    "}\n",
    "catch(err) {\n",
    "    // NoOp\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "instance_type": "ml.t3.medium",
  "kernelspec": {
   "display_name": "Python 3 (Data Science)",
   "language": "python",
   "name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-east-1:081325390199:image/datascience-1.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
